package cn.tju.yanbo.goodoccasion;


import java.sql.Date;
import java.util.Calendar;
import java.util.Random;

import cn.tju.yanbo.commons.BaseActivity;
import cn.tju.yanbo.commons.CalendarGestureListener;

import cn.tju.yanbo.R;
import cn.tju.yanbo.calendar.LunarCalendarActivity;
import cn.tju.yanbo.datechoose.DateChooseActivity;
import cn.tju.yanbo.homepage.HomepageActivity;
import cn.tju.yanbo.luckyday.LuckyDayActivity;
import cn.tju.yanbo.setting.SettingActivity;
import cn.tju.yanbo.utils.AppConfig;
import cn.tju.yanbo.utils.AppUtils;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class GoodOccasionActivity extends BaseActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.good_occasion);

        this.initActivity(savedInstanceState);
        
        View view = findViewById(R.id.main_content_view);
        view.setOnTouchListener(new CalendarGestureListener(this));
        
        this.setCustomFont(R.id.ji_xiong_view, AppUtils.font());
        this.setCustomFont(R.id.yi_view, AppUtils.font());
        this.setCustomFont(R.id.ji_view, AppUtils.font());
        this.update(Calendar.getInstance());
    }

    protected void registerIntent() {
        registerIntent(R.id.menu_homepage, HomepageActivity.class);
        registerIntent(R.id.menu_lunar_calendar, LunarCalendarActivity.class);
        registerIntent(R.id.menu_datechoose, GoodOccasionActivity.class);
        registerIntent(R.id.menu_lucky_day, LuckyDayActivity.class);
        registerIntent(R.id.menu_setting, SettingActivity.class);
    }

    @Override
    protected boolean hasDateView() {
        return true;
    }

    @Override
    protected Class<?> leftActivity() {
        return DateChooseActivity.class;
    }

    @Override
    protected Class<?> rightActivity() {
        return LuckyDayActivity.class;
    }
    
    @Override
    public int[] getRevolveRadius() {
        return AppConfig.DATE_RING_SERIES_RADIUS;
    }
    
    public int compassRadius() {
        return AppConfig.DATE_RING_SERIES_RADIUS[3];
    }
    
    @Override
    protected Drawable background() {
        return CACHED_PICTURES.get(R.drawable.good_occasion);
    }
    
    private static String [] JI_XIONG = new String[]{"吉", "凶"};
    public void update(Calendar c) {
        String sc = AppUtils.format("yyyy-MM-dd", c.getTime());
        Date date = Date.valueOf(sc);
        int hourOfDay = c.get(Calendar.HOUR_OF_DAY);
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        
        Random random = new Random(c.getTimeInMillis());
        String jiXiong = JI_XIONG[Math.abs(random.nextInt())%2];
        int yiji = Math.abs(random.nextInt());
        String lunarHourKey = AppUtils.lunarHour(c.get(Calendar.HOUR_OF_DAY));
        String lunarHour = AppConfig.get(lunarHourKey);
        String [] chongsha = lunarHour.split("-");
        String yi = AppConfig.YIJI_PIC_NAMES[yiji%AppConfig.YIJI_PIC_NAMES.length];
        String ji = AppConfig.YIJI_PIC_NAMES[(yiji+1)%AppConfig.YIJI_PIC_NAMES.length];
        
        this.setViewText(R.id.ji_xiong_view, jiXiong);
        this.setViewText(R.id.yi_view, chongsha[0]);
        this.setViewText(R.id.ji_view, chongsha[1]);
        
        ImageView yiView = (ImageView)this.findViewById(R.id.yi_pic);
        ImageView jiView = (ImageView)this.findViewById(R.id.ji_pic);
        yiView.setImageDrawable(this.CACHED_PICTURES.get("yi_"+yi+"_small"));
        jiView.setImageDrawable(this.CACHED_PICTURES.get("ji_"+ji+"_small"));
        
        Log.i("gui", "ji_xiong="+jiXiong+",yi="+yi+",ji="+ji);
    }
}
